Skip to content

[client] Add cpu profile to debug bundle#4700

Merged
lixmal merged 7 commits intomainfrom
cpu-profile
Jan 22, 2026
Merged

[client] Add cpu profile to debug bundle#4700
lixmal merged 7 commits intomainfrom
cpu-profile

Conversation

@lixmal
Copy link
Copy Markdown
Collaborator

@lixmal lixmal commented Oct 26, 2025

Describe your changes

Issue ticket number and link

Stack

Checklist

  • Is it a bug fix
  • Is a typo/documentation fix
  • Is a feature enhancement
  • It is a refactor
  • Created tests that fail without the change (if possible)

By submitting this pull request, you confirm that you have read and agree to the terms of the Contributor License Agreement.

Documentation

Select exactly one:

  • I added/updated documentation for this change
  • Documentation is not needed for this change (explain why)

Docs PR URL (required if "docs added" is checked)

Paste the PR link from https://github.com/netbirdio/docs here:

https://github.com/netbirdio/docs/pull/__

Summary by CodeRabbit

  • New Features
    • Start/stop CPU profiling on demand during debug sessions.
    • Captured CPU profiles are included in exported debug bundles (cpu.prof).
    • Debug workflow now starts profiling when the service is configured for debug and stops it when collection completes.

✏️ Tip: You can customize this high-level summary in your review settings.

Copilot AI review requested due to automatic review settings October 26, 2025 14:23
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR adds CPU profiling capability to the debug bundle by implementing RPC methods to start and stop profiling in the daemon, capturing the profile data during debug bundle collection.

  • Adds StartCPUProfile and StopCPUProfile RPC methods to the daemon service
  • Integrates CPU profiling into the debug bundle generation workflow
  • Captures profiling data during the debug bundle collection window

Reviewed Changes

Copilot reviewed 8 out of 8 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
client/proto/daemon.proto Defines new RPC methods and message types for CPU profiling
client/proto/daemon.pb.go Generated protobuf code for CPU profiling messages
client/proto/daemon_grpc.pb.go Generated gRPC client/server code for CPU profiling methods
client/server/server.go Adds fields to track CPU profiling state and buffer
client/server/debug.go Implements StartCPUProfile and StopCPUProfile RPC handlers
client/internal/debug/debug.go Integrates CPU profile data into debug bundle archive
client/ui/debug.go Starts CPU profiling before collecting debug data
client/cmd/debug.go Adds CPU profiling lifecycle management to CLI debug command

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread client/server/debug.go Outdated
Comment thread client/server/debug.go
Comment thread client/cmd/debug.go
@sonarqubecloud
Copy link
Copy Markdown

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Nov 26, 2025

Note

Other AI code review bot(s) detected

CodeRabbit has detected other AI code review bot(s) in this pull request and will avoid duplicating their findings in the review comments. This may lead to a less comprehensive review.

📝 Walkthrough

Walkthrough

Adds CPU profiling to the debug workflow: new StartCPUProfile/StopCPUProfile RPCs, server-side profiling state and pprof handling, CLI/UI lifecycle integration to start/stop profiling, and bundling of captured cpu.prof into debug archives via a new GeneratorDependencies field.

Changes

Cohort / File(s) Summary
Daemon RPC Interface
client/proto/daemon.proto
Added StartCPUProfile and StopCPUProfile RPCs and corresponding request/response messages to control CPU profiling.
Server state & handlers
client/server/server.go,
client/server/debug.go
Added cpuProfileBuf *bytes.Buffer and cpuProfiling bool to Server; implemented StartCPUProfile and StopCPUProfile RPC handlers with mutex protection, pprof start/stop, buffer capture, and inclusion of CPUProfile in DebugBundle.
CLI debug lifecycle
client/cmd/debug.go
Updated runForDuration to attempt StartCPUProfile, track whether profiling started, ensure StopCPUProfile is called, and pass captured profile (nil when unavailable) into debug bundle generation.
Debug bundle generation
client/internal/debug/debug.go
Added CPUProfile []byte to GeneratorDependencies and BundleGenerator state; implemented addCPUProfile to write cpu.prof into the archive (non-fatal on failure).
UI integration
client/ui/debug.go
Calls StartCPUProfile after service startup and StopCPUProfile after debug collection; logs warnings on RPC failures.

Sequence Diagram(s)

sequenceDiagram
    participant UI as UI/CLI
    participant Server as Server
    participant Profiler as pprof
    participant Bundle as Debug Bundle

    UI->>Server: StartCPUProfile()
    activate Server
    Server->>Server: lock mutex\nif profiling -> return error\ninit buffer, cpuProfiling=true
    Server->>Profiler: pprof.StartCPUProfile(buffer)
    Server-->>UI: StartCPUProfileResponse
    deactivate Server

    Note over Server,Profiler: CPU profiling runs, data written to buffer

    UI->>UI: collect debug data / runForDuration elapses

    UI->>Server: StopCPUProfile()
    activate Server
    Server->>Server: lock mutex\nif not profiling -> return error\npprof.StopCPUProfile(), cpuProfiling=false
    Server-->>UI: StopCPUProfileResponse
    deactivate Server

    UI->>Server: Request DebugBundle()
    activate Server
    Server->>Bundle: Provide DebugBundle request (includes CPUProfile bytes)
    Bundle->>Bundle: addCPUProfile() -> write cpu.prof into archive
    Bundle-->>UI: debug bundle (with cpu.prof)
    deactivate Server
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Possibly related PRs

Suggested reviewers

  • pascal-fischer
  • mlsmaycon

Poem

🐰 I nibble at traces, I hop through each thread,

I start then I stop, and I stash what I read.
A tiny cpu.prof snug in the crate,
For sleuths who will parse how cycles create.
🥕📦

🚥 Pre-merge checks | ✅ 2 | ❌ 3
❌ Failed checks (3 warnings)
Check name Status Explanation Resolution
Description check ⚠️ Warning The description is incomplete. While the template structure is present and the feature enhancement checkbox is marked, the 'Describe your changes' section is empty, the 'Issue ticket number and link' section is missing, and no explanation is provided for why documentation is not needed. Fill in the 'Describe your changes' section with details about the CPU profiling implementation, add the issue ticket number and link, and provide an explanation for why documentation is not needed.
Linked Issues check ⚠️ Warning The PR objectives note that no issue ticket number or link is provided in the description, and the 'Issue ticket number and link' section is empty. Add the related issue ticket number and link to the PR description to establish traceability.
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately describes the main change: adding CPU profiling to the debug bundle, which is evident from all modified files implementing this feature.
Out of Scope Changes check ✅ Passed All changes are directly related to the stated objective of adding CPU profiling to the debug bundle. Changes span proto definitions, server implementation, UI integration, and debug bundle generation—all in scope.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing touches
  • 📝 Generate docstrings

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@sonarqubecloud
Copy link
Copy Markdown

sonarqubecloud Bot commented Nov 26, 2025

Quality Gate Passed Quality Gate passed

Issues
0 New issues
2 Accepted issues

Measures
0 Security Hotspots
No data about Coverage
0.0% Duplication on New Code

See analysis details on SonarQube Cloud

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between aca0398 and 5a7045e.

⛔ Files ignored due to path filters (2)
  • client/proto/daemon.pb.go is excluded by !**/*.pb.go
  • client/proto/daemon_grpc.pb.go is excluded by !**/*.pb.go
📒 Files selected for processing (6)
  • client/cmd/debug.go (3 hunks)
  • client/internal/debug/debug.go (6 hunks)
  • client/proto/daemon.proto (2 hunks)
  • client/server/debug.go (4 hunks)
  • client/server/server.go (2 hunks)
  • client/ui/debug.go (2 hunks)
🧰 Additional context used
🧬 Code graph analysis (1)
client/ui/debug.go (1)
client/proto/daemon.pb.go (6)
  • StartCPUProfileRequest (5230-5234)
  • StartCPUProfileRequest (5247-5247)
  • StartCPUProfileRequest (5262-5264)
  • StopCPUProfileRequest (5304-5308)
  • StopCPUProfileRequest (5321-5321)
  • StopCPUProfileRequest (5336-5338)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (17)
  • GitHub Check: Relay / Unit (amd64, -race)
  • GitHub Check: Management / Unit (amd64, postgres)
  • GitHub Check: Signal / Unit (386)
  • GitHub Check: Relay / Unit (386)
  • GitHub Check: Management / Unit (amd64, sqlite)
  • GitHub Check: Management / Unit (amd64, mysql)
  • GitHub Check: Client / Unit (386)
  • GitHub Check: Client / Unit (amd64)
  • GitHub Check: Client (Docker) / Unit
  • GitHub Check: JS / Lint
  • GitHub Check: release
  • GitHub Check: Client / Unit
  • GitHub Check: Windows
  • GitHub Check: Linux
  • GitHub Check: Android / Build
  • GitHub Check: iOS / Build
  • GitHub Check: Client / Unit
🔇 Additional comments (12)
client/ui/debug.go (2)

412-414: LGTM - CPU profiling integration is appropriately non-blocking.

The error is logged as a warning and the debug flow continues, which is correct since CPU profiling is an optional enhancement to the debug bundle.


457-459: LGTM - StopCPUProfile placement is correct.

Stopping CPU profiling after the duration wait and before collecting final status ensures the profile captures the entire debug collection period.

client/internal/debug/debug.go (2)

533-544: LGTM - Clean implementation following existing patterns.

The addCPUProfile method correctly:

  • Returns early when no CPU profile data is available
  • Uses the same addFileToZip pattern as other profile methods
  • Provides appropriate error wrapping

334-336: LGTM - CPU profile addition integrates well with existing archive creation flow.

The CPU profile is added after the standard runtime profiles, with error logging that doesn't interrupt bundle generation.

client/server/server.go (1)

85-87: LGTM - CPU profiling state fields properly added to Server struct.

The fields are appropriately scoped within the Server struct and will be protected by the existing mutex as shown in the debug.go implementation.

client/server/debug.go (3)

37-43: LGTM - Safe CPU profile data extraction with proper cleanup.

The deferred cleanup correctly nils the buffer reference after bundle generation completes. Since Bytes() returns a slice referencing the buffer's internal array, and the cleanup only nils the pointer (not the underlying data), this is safe.


230-249: LGTM - StartCPUProfile implementation is thread-safe.

Good implementation:

  • Sets cpuProfiling = true before calling pprof.StartCPUProfile (addressing the previous review feedback)
  • Proper cleanup on error (resets both buffer and flag)
  • Mutex protection throughout

252-268: LGTM - StopCPUProfile correctly preserves the buffer for later retrieval.

The implementation properly stops profiling and resets the flag while preserving the buffer data for DebugBundle to consume later.

client/proto/daemon.proto (2)

96-101: LGTM - RPC definitions follow existing conventions.

The new CPU profiling RPCs are well-documented and follow the established patterns in the service definition.


764-775: LGTM - Message definitions are appropriately minimal.

Empty request/response messages are the right choice for these simple control operations where no parameters or return data are needed beyond success/failure.

client/cmd/debug.go (2)

223-235: Well-designed cleanup pattern for CPU profiling.

The defer ensures profiling is stopped in case of early return (e.g., if waitForDurationOrCancel fails at line 241), while the flag-based guard prevents attempting to stop profiling that never started or was already stopped. This is solid defensive programming.

Note: The explicit stop at lines 245-250 is intentional and necessary—it must execute before the bundle is created so the profile data is finalized and available to the DebugBundle RPC.


403-403: Correct: CPU profiling not applicable in SIGUSR1 flow.

Setting CPUProfile: nil is appropriate here. The generateDebugBundle function handles signal-based (SIGUSR1) debug bundle generation without CPU profiling, whereas the runForDuration flow uses the RPC-based DebugBundle call where the daemon server manages CPU profile inclusion.

Comment thread client/cmd/debug.go Outdated
Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Fix all issues with AI agents
In `@client/server/debug.go`:
- Around line 29-35: The DebugBundle code currently reads
s.cpuProfileBuf.Bytes() which can race with pprof background writes; update
DebugBundle to check the cpuProfiling boolean flag (the same flag set by
StartCPUProfile/StopCPUProfile) and skip capturing/including cpuProfileData when
cpuProfiling is true (i.e., profiling still active), only reading
s.cpuProfileBuf.Bytes() when cpuProfiling is false; keep the existing deferred
s.cpuProfileBuf = nil behavior when you do capture, and ensure you reference
s.cpuProfileBuf, cpuProfiling, DebugBundle, StartCPUProfile and StopCPUProfile
to locate and fix the code paths.
♻️ Duplicate comments (1)
client/cmd/debug.go (1)

241-247: Remove the redundant StopCPUProfile call.
There’s both a deferred stop and an explicit stop; keep one to avoid double-stop.

Comment thread client/server/debug.go
@sonarqubecloud
Copy link
Copy Markdown

@lixmal lixmal merged commit d0221a3 into main Jan 22, 2026
38 checks passed
@lixmal lixmal deleted the cpu-profile branch January 22, 2026 11:24
@coderabbitai coderabbitai Bot mentioned this pull request Jan 23, 2026
7 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants